package main.java.ejb.student;

import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import main.java.enity.Student;

/**
 * Session Bean implementation class StudentBean
 */
@Stateless(mappedName= "student")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class StudentBean implements StudentEJB {

        @PersistenceContext(unitName = "Project3Final")
        private EntityManager em;
    /**
     * Default constructor. 
     */
    public StudentBean() {
        // TODO Auto-generated constructor stub
    }


        @Override
        public int AddStudent(String Name, String Address, String Email) {
                // TODO Auto-generated method stub
                try {
                        Student student = new Student();
                        
                        student.setName(Name);
                        student.setAddress(Address);
                        student.setEmail(Email);
                        
                        em.persist(student);

                        em.flush();
                        return student.getStudentId();

                } catch (Exception ex) {
                        return -1;
//                      throw new EJBException("AddStudent: " + ex.getMessage());
                }
        }


        @Override
        public boolean DeleteStudent(int StudentID) {
                // TODO Auto-generated method stub
                try {
                        Student student = em.find(Student.class, StudentID);

                        if (student == null) {
                                throw new Exception("student not found");
                        }

                        em.remove(student);
                        return true;
                } catch (Exception ex) {
                        return false;
                        //throw new EJBException("removeCustomer: " + ex.getMessage());
                }
        }


        @Override
        public boolean UpdateStudent(int StudentID, String Name, String Address,
                        String Email) {
                // TODO Auto-generated method stub
                try {
                        Student student = em.find(Student.class, StudentID);
                        
                        student.setName(Name);
                        student.setAddress(Address);
                        student.setEmail(Email);
                        
                        em.merge(student);
                        return true;
                } catch (Exception ex) {
                        return false;
//                      throw new EJBException("createCustomer: " + ex.getMessage());
                }
        }

        @Override
        @SuppressWarnings("rawtypes")
        public List findAllStudent() {
                try {
                        String querryStr = "SELECT a FROM Student a";
                        Query query = em.createQuery(querryStr);
                        return query.getResultList();
                } catch (Exception ex) {
                        return null;
//                      throw new EJBException("findAllStudent: " + ex.getMessage());
                }               
        }


        @Override
        @SuppressWarnings("rawtypes")
        public List findStudentByName(String Name) {
                try {
                        String queryStr = "SELECT a FROM Student a WHERE a.name = :Name";
                        Query query = em.createQuery(queryStr);
                        query.setParameter("Name", Name);
                        return query.getResultList();
                } catch (Exception ex) {
                        //return null;
                        throw new EJBException("findAllStudent: " + ex.getMessage());
                }
        }

}
